import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    private int[] elem;
    private int usedSize;

    private static final int DEAULT_CAPCITY = 10;

    public MyStack() {
        this.elem = new int[DEAULT_CAPCITY];
    }


    //添加
    public void push(int val){
        if(isFull()){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[usedSize] = val;
        this.usedSize++;
    }

    public boolean isFull(){
       return usedSize == elem.length;
    }


    public int pop(){
        if (isEmpty()){
            throw new EmptyException("栈为空");
        }else {
            int oldVal = elem[usedSize - 1];
            usedSize--;
            return oldVal;
        }
    }


    public int peek(){
        if (isEmpty()){
            throw new EmptyException("栈为空");
        }
        int oldVal = elem[usedSize-1];
        return oldVal;
    }

    public boolean isEmpty(){
       return usedSize == 0;
    }

    //括号匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch =='(' || ch =='{' || ch =='['){
                stack.push(ch);
            }else {
                //右括号
                //1.判断栈里面没有左括号
                if(stack.isEmpty()){
                    return false;
                }
                //2.此时top是左括号，ch 是右括号
                char top = stack.peek();
                if(top == '(' && ch ==')' || top == '{' && ch =='}' || top == '[' && ch ==']'){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        //如果循环走完了,stack 不为空
        if (!stack.isEmpty()){
            return false;
        }
        return true;
    }



    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(!isOpertion(tokens[i])){
                String x = tokens[i];
                stack.push(Integer.valueOf(x));
            }else {
                // 有可能是加减乘除 当中的一个运算符
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (tokens[i]){
                    case "+":
                        stack.push(num1 + num2);
                        break;

                    case "-":
                        stack.push(num1 - num2);
                        break;

                    case "*":
                        stack.push(num1 * num2);
                        break;

                    case "/":
                        stack.push(num1 / num2);
                        break;

                }
            }

        }
        return stack.pop();
    }

    private boolean isOpertion(String s){
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            return true;
        }
        return false;
    }



    // 出栈入栈次序匹配
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            int j = 0;
            while(!stack.isEmpty() && j < popV.length && stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }
}
